var area_picker = require("./../../components/area-picker/area-picker.js"), app = getApp(), api = getApp().api;
function setOnShowScene(e) {
  app.onShowData || (app.onShowData = {}), app.onShowData.scene = e;
}


Page({
    data: {
        is_form_show: !1,
        business: [],
        certificate: [],
        card_front: null,
        card_obverse: null,
        card_obverse: null,
        apply_type: 0,
        o_type: 2401,
        business_addition_pics:null,
        qualifications:null,
        id_doc_copy:null,
        organization_copy:null,
        o_type_name:'小微商户',
        isChecked: false,
        organization_type:[
          {
            id: 2401,
            name: '小微商户'
          },
          {
            id: 2500,
            name: '个人卖家'
          },
          {
            id: 4,
            name: '个体工商户'
          },
          {
            id: 2,
            name: '企业'
          },
          {
            id: 3,
            name: '党政、机关及事业单位，包括国内各级、各类政府机构、事业单位等'
          },
          {
            id: 1708,
            name: '其他组织'
          },
        ],
        id_type: 'IDENTIFICATION_TYPE_MAINLAND_IDCARD',
        id_type_name: '中国大陆居民-身份证',
        id_doc_type: [
          {
            id: 'IDENTIFICATION_TYPE_MAINLAND_IDCARD',
            name: '中国大陆居民-身份证'
          },
          {
            id: 'IDENTIFICATION_TYPE_OVERSEA_PASSPORT',
            name: '其他国家或地区居民-护照'
          },
          {
            id: 'IDENTIFICATION_TYPE_HONGKONG',
            name: '中国香港居民–来往内地通行证'
          },
          {
            id: 'IDENTIFICATION_TYPE_MACAO',
            name: '中国澳门居民–来往内地通行证'
          },
          {
            id: 'IDENTIFICATION_TYPE_TAIWAN',
            name: '中国台湾居民–来往大陆通行证'
          },
        ],
        c_type: '65',
        c_type_name: '65-经营者/法人',
        contact_type: [
          {
            id: 65,
            name: '65-经营者/法人'
          },
          {
            id: 66,
            name: '66-负责人'
          },
        ],
        b_type: '75',
        b_type_name: '75-对私账户',
        bank_account_type: [
          {
            id: 75,
            name: '75-对私账户'
          },
          {
            id: 74,
            name: '74-对公账户'
          },
        ],
    },
    onLoad: function(e) {
      getCurrentPages().length < 2 && this.setData({
        show_index: !0
      });
        getApp().page.onLoad(this, e);
        if (this.data.store.is_shenhe == 0) {
          wx.setNavigationBarTitle({
            title: '申请'
          })
        }
        var t = this;
        t.getDistrictData(function(e) {
            area_picker.init({
                page: t,
                data: e
            });
        }), getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        }), getApp().request({
            url: getApp().api.mch.apply,
            success: function(e) {
                getApp().core.hideLoading(), 0 == e.code && (e.data.apply && (e.data.show_result = !0), 
                t.setData(e.data), e.data.apply || t.setData({
                    is_form_show: !0
                }));
                if(e.data.is_dspay){
                  t.setData({
                    is_dspay: e.data.is_dspay,
                  });
                }
              if (e.data.apply) {
                t.setData({
                  is_form_show: true,
                  business_addition_pics: e.data.apply.business_addition_pics,
                  qualifications:e.data.apply.qualifications,
                  business_addition_desc:e.data.apply.business_addition_desc,
                  card_front: e.data.apply.card_front,
                  card_obverse: e.data.apply.card_obverse,
                  business: [e.data.apply.business_license_copy],
                  apply: e.data.apply,
                  sign_info: e.data.sign_info,
                  o_type_name:e.data.apply.organization_type,
                  o_type:e.data.apply.o_type,
                  organization_copy:e.data.apply.organization_copy,
                });
              }
            }
        });
    },
    getDistrictData: function(t) {
        var a = getApp().core.getStorageSync(getApp().const.DISTRICT);
        if (!a) return getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        }), void getApp().request({
            url: getApp().api.default.district,
            success: function(e) {
                getApp().core.hideLoading(), 0 == e.code && (a = e.data, getApp().core.setStorageSync(getApp().const.DISTRICT, a), 
                t(a));
            }
        });
        t(a);
    },
    onAreaPickerConfirm: function(e) {
        this.setData({
            edit_district: {
                province: {
                    id: e[0].id,
                    name: e[0].name
                },
                city: {
                    id: e[1].id,
                    name: e[1].name
                },
                district: {
                    id: e[2].id,
                    name: e[2].name
                }
            }
        });
    },
    checkboxChange: function(e) {
      console.log(e)
      e.detail.value.length>0?this.setData({
        agree_entry_rules: !0,
        show_entry_rules: !1
      }):this.setData({
        agree_entry_rules: !1,
        show_entry_rules: !1
      })
    },
    onReady: function() {
        getApp().page.onReady(this);
    },
    onShow: function() {
        getApp().page.onShow(this);
    },
    onHide: function() {
        getApp().page.onHide(this);
    },
    onUnload: function() {
        getApp().page.onUnload(this);
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    mchCommonCatChange: function(e) {
        this.setData({
            mch_common_cat_index: e.detail.value
        });
    },
    organizationType: function (e) {
      var o_type=this.data.organization_type[e.detail.value].id
      var o_type_name = this.data.organization_type[e.detail.value].name
       this.setData({
         o_type: o_type,
         o_type_name: o_type_name
       });
     },
     idDocType: function (e) {
       var id_type = this.data.id_doc_type[e.detail.value].id
       var id_type_name = this.data.id_doc_type[e.detail.value].name
       this.setData({
         id_type: id_type,
         id_type_name: id_type_name
       });
     },
     contactType: function (e) {
       var c_type = this.data.contact_type[e.detail.value].id
       var c_type_name = this.data.contact_type[e.detail.value].name
       this.setData({
         c_type: c_type,
         c_type_name: c_type_name
       });
     },
     bankAccountType: function (e) {
       var b_type = this.data.bank_account_type[e.detail.value].id
       var b_type_name = this.data.bank_account_type[e.detail.value].name
       this.setData({
         b_type: b_type,
         b_type_name: b_type_name
       });
     },
     radioChange: function (e) {
       this.setData({
         apply_type: e.detail.value
       })
     },
    applySubmit: function(e) {
        var t = this;
        // !t.data.entry_rules || t.data.agree_entry_rules ? (
        getApp().core.showLoading({
            title: "正在提交",
            mask: !0
        }), 0 === t.data.mch_common_cat_index && (t.data.mch_common_cat_index = "0"), getApp().request({
            url: getApp().api.mch.apply_submit,
            method: "post",
            data: {
                realname: e.detail.value.realname,
                tel: e.detail.value.tel,
                name: e.detail.value.name,
                province_id: e.detail.value.province_id,
                city_id: e.detail.value.city_id,
                district_id: e.detail.value.district_id,
                address: e.detail.value.address,
                mch_common_cat_id: t.data.mch_common_cat_index ? t.data.mch_common_cat_list[t.data.mch_common_cat_index].id : t.data.apply && t.data.apply.mch_common_cat_id ? t.data.apply.mch_common_cat_id : "",
                service_tel: e.detail.value.service_tel,
                form_id: e.detail.formId,
                wechat_name: e.detail.value.wechat_name,
              business: t.data.business[0] ? t.data.business[0]  : '',
              certificate: t.data.certificate ? JSON.stringify(t.data.certificate) : '',
              card_front: t.data.card_front ? t.data.card_front : '',
              card_obverse: t.data.card_obverse ? t.data.card_obverse : '',
              apply_price: t.data.apply_price ? t.data.apply_price : 0,
              apply_type: 0,//t.data.apply_type
                //电商收付通需要
                account_bank: e.detail.value.account_bank,
                account_name: e.detail.value.account_name,
                account_number: e.detail.value.account_number,
                bank_account_type:t.data.b_type,
                bank_address_code: e.detail.value.bank_address_code,
                business_addition_desc: e.detail.value.business_addition_desc ? e.detail.value.business_addition_desc : '',
                business_license_number: e.detail.value.business_license_number ? e.detail.value.business_license_number : '',
                business_time: e.detail.value.business_time ? e.detail.value.business_time : '',
                company_address: e.detail.value.company_address ? e.detail.value.company_address : '',
                contact_type: t.data.c_type,
                contact_email: e.detail.value.contact_email,
                contact_id_card_number: e.detail.value.contact_id_card_number,
                contact_name: e.detail.value.contact_name,
                id_card_num: e.detail.value.id_card_num,
                id_card_name: e.detail.value.id_card_name,
                id_card_valid_time: e.detail.value.id_card_valid_time,
                legal_person: e.detail.value.legal_person ? e.detail.value.legal_person : '',
                merchant_name: e.detail.value.merchant_name ? e.detail.value.merchant_name : '',
                mobile_phone: e.detail.value.mobile_phone ? e.detail.value.mobile_phone : '',
                organization_number: e.detail.value.organization_number ? e.detail.value.organization_number : '',
                organization_type: t.data.o_type ? t.data.o_type : '',
                organization_time: e.detail.value.organization_time ? e.detail.value.organization_time : '',
                id_doc_number: e.detail.value.id_doc_number ? e.detail.value.id_doc_number : '',
                id_doc_name: e.detail.value.id_doc_name ? e.detail.value.id_doc_name : '',
                doc_period_end: e.detail.value.doc_period_end ? e.detail.value.doc_period_end : '',
                id_doc_copy: t.data.id_doc_copy ? t.data.id_doc_copy : '',
                id_doc_type: t.data.id_type ? t.data.id_type : '',
                business_addition_pics: t.data.business_addition_pics ? t.data.business_addition_pics : '',
                qualifications: t.data.qualifications ? t.data.qualifications : '',
                organization_copy: t.data.organization_copy ? t.data.organization_copy : '',
            },
            success: function(res) {
              if( t.data.apply_price > 0) {
                console.log(t.data.certificate)
                if (0 == res.code){
                  return setTimeout(function() {
                    getApp().core.hideLoading()
                  }, 1e3), setOnShowScene("pay"), void wx.requestPayment({
                      _res: res,
                      timeStamp: res.data.timeStamp,
                      nonceStr: res.data.nonceStr,
                      package: res.data.package,
                      signType: res.data.signType,
                      paySign: res.data.paySign,
                      success: function(e) {},
                      fail: function(e) {},
                      complete: function(e) {
                          "requestPayment:fail" != e.errMsg && "requestPayment:fail cancel" != e.errMsg ? wx.showModal({
                              title: "提示",
                              content: "申请成功",
                              showCancel: !1,
                              confirmText: "确认",
                              success: function(e) {
                                  e.confirm && wx.navigateBack({
                                      delta: 1
                                  });
                              }
                          }) : wx.showModal({
                              title: "提示",
                              content: "订单尚未支付",
                              showCancel: !1,
                              confirmText: "确认"
                          });
                      }
                  });
                } else {
                  wx.showModal({
                    title: "提示",
                    content: res.msg,
                    showCancel: !1
                  }), wx.hideLoading();
                }
              
            } else {
              getApp().core.hideLoading(), 0 == res.code && getApp().core.showModal({
                    title: "提示",
                    content: res.msg,
                    showCancel: !1,
                    success: function(t) {
                        t.confirm && wx.redirectTo({
                            url: "/mch/apply/apply-info"
                        });
                    }
                }), 1 == res.code && getApp().core.showToast({
                    title: res.msg
                });

            }
              
            }
        })
        // ) : getApp().core.showModal({
        //     title: "提示",
        //     content: "请先阅读并同意《入驻协议》。",
        //     showCancel: !1
        // });
    },
    hideApplyResult: function() {
        this.setData({
            show_result: !1,
            is_form_show: !0
        });
    },
    showApplyResult: function() {
        this.setData({
            show_result: !0
        });
    },
    showEntryRules: function() {
        this.setData({
            show_entry_rules: !0
        });
    },
    disagreeEntryRules: function() {
        this.setData({
            agree_entry_rules: !1,
            show_entry_rules: !1,
            isChecked: false
        });
    },
    agreeEntryRules: function() {
        this.setData({
            agree_entry_rules: !0,
            show_entry_rules: !1,
            isChecked: true
        });
    },
    // 选择营业执照
  onChooseImage: function (e) {
    var type = e.currentTarget.dataset.type;
    var self = this;
    getApp().core.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function (a) {
        getApp().core.showLoading({
          type: "loading"
        });
        getApp().core.uploadFile({
          url: getApp().api.default.upload_image,
          filePath: a.tempFilePaths[0],
          name: "image",
          formData: {
            is_mch_apply: 1
           },
          success: function (res) {
            getApp().core.hideLoading();
            var res = JSON.parse(res.data);
            if (res.code == 0) {
              if (type == 'business') {
                self.setData({
                  business: [res.data.url]
                });
              }
              if (type == 'card_front') {
                self.setData({
                  card_front: res.data.url
                });
              }
              if (type == 'card_obverse') {
                self.setData({
                  card_obverse: res.data.url
                });
              }
              if (type == 'id_doc_copy') {
                self.setData({
                  id_doc_copy: res.data.url
                });
              }
              if (type == 'organization_copy') {
                self.setData({
                  organization_copy: res.data.url
                });
              }
              if (type == 'qualifications') {
                self.setData({
                  qualifications: res.data.url
                });
              }
              if (type == 'business_addition_pics') {
                self.setData({
                  business_addition_pics: res.data.url
                });
              }
              
            } else {
              wx.showModal({
                title: '提示',
                content: res.msg,
                showCancel: false
              });
            }
          }
        });
      }
    });
  },
  add_img: function() {
    var t = this, a = 9 - t.data.certificate.length;
    getApp().core.chooseImage({
        count: a,
        sizeType: [ "original", "compressed" ],
        sourceType: [ "album", "camera" ],
        success: function(a) {
            var e = a.tempFilePaths,certificate=t.data.certificate;

            e.forEach(function(a, s) {
                getApp().core.showLoading({
                    type: "loading"
                }), getApp().core.uploadFile({
                    url: getApp().api.default.upload_image,
                    filePath: a,
                    name: "image",
                    formData: {
                      is_mch_apply: 1
                    },
                    success: function(a) {
                      getApp().core.hideLoading()
                      var res = JSON.parse(a.data);
                        if(0 == res.code){
                          certificate.push(res.data.url)
                          if(s==e.length-1){
                            t.setData({
                              certificate: certificate
                            })
                            console.log(t.data.certificate)
                          }
                        }
                    }
                });
            });
            
        }
    });
  },
  deleteImage: function(t) {
    var o = t.currentTarget.dataset.picIndex,type = t.currentTarget.dataset.type;
    
    if(type == 'business'){
      var a = this.data.business;
      a.splice(o, 1), this.setData({
        business: a
      });
    }else{
      var a = this.data.certificate;
      a.splice(o, 1), this.setData({
        certificate: a
      });
    }
  },
});